package com.intelligentmeterreadingsys.BaseDao;

import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.intelligentmeterreadingsys.util.SharedPreferenceUtil;

import dbhelper.DBOpenHelper;
import dbhelper.XmlNameHelper;

/**
 * 数据库基础类
 * 
 * @author king
 * 
 */
public class BaseDao {

    private SQLiteDatabase db;

    private DBOpenHelper helper;

    private String userId;

    public SQLiteDatabase getDb() {
	return db;
    }

    public void setDb(SQLiteDatabase db) {
	this.db = db;
    }

    public BaseDao(Context context) {
	helper = new DBOpenHelper(context);
	SharedPreferenceUtil sh = new SharedPreferenceUtil(context);
	this.userId = sh.getValue(XmlNameHelper.userID);
	db = helper.getWritableDatabase();
    }

    /**
     * 批量添加数据
     * 
     * @param value
     *            数据
     * @param table
     *            表名
     * @return
     */
    public boolean insertALL(List<ContentValues> value, String table) {

	db.beginTransaction();
	for (int i = 0; i < value.size(); i++) {
	    long id = db.insert(table, null, value.get(i));

	    if (id < 1) {

		return false;
	    }

	}
	db.setTransactionSuccessful();
	db.endTransaction();
	return true;

    }

    /**
     * 添加一条数据
     * 
     * @param values
     * @param table
     * @return
     */
    public boolean insert(ContentValues values, String table) {
	boolean b = false;
	long id = db.insert(table, null, values);
	if (id > 1) {
	    b = true;
	}
	return b;
    }

    /**
     * 删除所有数据
     * 
     * @param table
     * @return
     */
    public boolean delectAll(String table) {
	boolean b = false;
	long id = db.delete(table, "_id>?", new String[] { "0" });
	if (id > 0) {
	    b = true;
	}
	return b;
    }

    /**
     * 删除一条数据
     * 
     * @param id
     * @param table
     * @return
     */
    public boolean delect(String id, String table) {
	boolean b = false;
	long ids = db.delete(table, "_id=?", new String[] { id });
	if (ids > 0) {
	    b = true;
	}
	return b;
    }

    /**
     * 通过ID更新一条数据
     * 
     * @param id
     * @param table
     * @param values
     * @return
     */
    public boolean modify(String id, String table, ContentValues values) {
	boolean b = false;

	long k = db.update(table, values, "_id=?", new String[] { id });
	if (k > 0) {
	    b = true;
	}
	return b;
    }

    /**
     * 查询总条数
     * 
     * @param db
     * @param sql
     * @return
     */
    public int getCount(String sql) {
	int i = 0;

	Cursor cursor = db.rawQuery(sql, null);

	if (cursor.moveToNext()) {
	    i = cursor.getCount();
	}
	if (cursor != null) {
	    cursor.close();
	}
	return i;

    }

    /**
     * 分页
     * 
     * @param db
     * @param firstResult
     *            从第几条数据开始查询
     * @param maxResult
     *            每页显示多少条记录
     * @param sql
     * @return
     */
    public Cursor getAllItems(int firstResult, int maxResult, String sql) {

	sql = sql + " limit ?,?";

	Cursor mCursor = db.rawQuery(sql, new String[] { String.valueOf(firstResult), String.valueOf(maxResult) });
	return mCursor;
    }

    /**
     * 通过ID批量修改数据
     * 
     * @author king
     * @param map
     *            key:ID,
     * @param tableName
     *            表名
     */
    public boolean modifyAll(Map<String, ContentValues> map, String tableName) {

	db.beginTransaction();
	for (String values : map.keySet()) {
	    int k = db.update(tableName, map.get(values), "_id=?", new String[] { values });
	    if (k < 1) {
		return false;
	    }
	}
	db.setTransactionSuccessful();
	db.endTransaction();
	return true;

    }

    /**
     * 关闭连接
     */
    public void close() {
	if (db != null && db.isOpen()) {
	    db.close();
	}
    }

    public String getUserId() {
	return userId;
    }

    public void setUserId(String userId) {
	this.userId = userId;
    }
}
